package fastwave.cloud.demo.fastwave.biz.publisher.config;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.SerializerMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

/**
 * <p>Title: TemplateConfig</p>
 * <p>Description: </p>
 *
 * @author: dwLiu
 * @date: 2020/4/8 21:47
 */
@Configuration
public class TemplateTransConfig {
    @Scope("prototype")
    @Bean(name = "TemplateTrans")
    public RabbitTemplate rabbitTemplateTrans(ConnectionFactory connectionFactory){
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setMessageConverter(new SerializerMessageConverter());
        return rabbitTemplate;
    }

    @Primary
    @Bean
    public RabbitTemplate createRabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        rabbitTemplate.setConnectionFactory(connectionFactory);
        //设置开启Mandatory,才能触发回调函数，无论消息结果推送怎么样都强制调用回调函数
        rabbitTemplate.setMandatory(true);

        //在exchange环节返回状态，是否有交换机，返回true,false
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                System.out.println("ConfirmCallback:    相关数据：" + correlationData);
                System.out.println("ConfirmCallback:    确认情况：" + ack);
                //channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'noExchange' in vhost '/', class-id=60, method-id=40)
                System.out.println("ConfirmCallback:    原因：" + cause);
            }
        });
        //在正确找到交换机（exchange）后，向队列（queue）路由的时候，没有正确的路由也就是没有任何的队列路由到队列区里面
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
                //(Body:'aa' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, deliveryTag=0])
                System.out.println("ReturnCallback:     消息：" + message);
                System.out.println("ReturnCallback:     回应码：" + replyCode);//312
                System.out.println("ReturnCallback:     回应信息：" + replyText);//NO_ROUTE
                System.out.println("ReturnCallback:     交换机：" + exchange);
                System.out.println("ReturnCallback:     路由键：" + routingKey);
            }
        });
        return rabbitTemplate;
    }
}
